package com.ruoyi.job.task;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.constant.ArticleConstant;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.system.api.RemoteArticleService;
import com.ruoyi.system.api.RemoteSearchService;
import com.ruoyi.system.api.domain.Article;
import com.ruoyi.system.api.domain.ArticleResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Component("articleTask")
public class ArticleTask {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Autowired
    RemoteSearchService remoteSearchService;

    @Autowired
    RemoteArticleService remoteArticleService;

    public void set(){
        redisTemplate.opsForValue().set("1111111","11111111");
    }

    public void update(){
        System.out.println("调用了11111111111111");
        //获取redis里所有的文章id
        Set<String> members =
                redisTemplate.opsForSet().members(ArticleConstant.ARTICLE_KEY);

        for (String member : members) {
            //更新es和数据库
            Article article = new Article();
            article.setId(Long.parseLong(member));
            Long hot = redisTemplate.opsForHash().size(ArticleConstant.HOT_KEY + member);
            article.setArticleHot(hot);
            remoteArticleService.innerUpdate(article, SecurityConstants.INNER);
            //更新es
            ArticleResponseVo articleResponseVo = new ArticleResponseVo();
            articleResponseVo.setId(Long.parseLong(member));
            articleResponseVo.setArticleHot(hot);
            remoteSearchService.update(articleResponseVo,SecurityConstants.INNER);
            //计算今日热榜
            Object o = redisTemplate.opsForHash().get(ArticleConstant.HOT_KEY, member);

            redisTemplate.opsForZSet().add(ArticleConstant.HOT_RANGE_KEY, member,Double.parseDouble(hot+""));

        }
    }

    public void update1(){
        Set<String> range = redisTemplate.opsForZSet().range(ArticleConstant.HOT_RANGE_KEY, ArticleConstant.INTEGERS[0], ArticleConstant.INTEGERS[1]);

        for (String s : range) {
            System.out.println(s);
            Double score = redisTemplate.opsForZSet().score(ArticleConstant.HOT_RANGE_KEY, s);
            if (score==null) continue;
            //更新es和数据库
            Article article = new Article();
            article.setId(Long.parseLong(s));
            article.setArticleHot(score.longValue());
            remoteArticleService.innerUpdate(article, SecurityConstants.INNER);
            //更新es
            ArticleResponseVo articleResponseVo = new ArticleResponseVo();
            articleResponseVo.setId(Long.parseLong(s));
            articleResponseVo.setArticleHot(score.longValue());
            remoteSearchService.update(articleResponseVo,SecurityConstants.INNER);
        }
    }
}
